package utils
{
	import flash.geom.Point;
	import flash.utils.ByteArray;
	
	public class StringUtil
	{
		 public static function format(str:String, ... rest):String{
	        if (str == null) return '';
	        var len:uint = rest.length;
	        var args:Array;
	        if (len == 1 && rest[0] is Array) {
	            args = rest[0] as Array;
	            len = args.length;
	        }else{
	            args = rest;
	        }
	        for (var i:int = 0; i < len; i++){
	            str = str.replace(new RegExp("\\{"+i+"\\}", "g"), args[i] == null ? "  " :  args[i]);
	        }
	        return str;
    	}
    	//去除前后空格
    	public static function trim(str:String):String{
	        if (str == null) return '';
	        
	        var startIndex:int = 0;
	        while (isWhitespace(str.charAt(startIndex)))
	            ++startIndex;
	
	        var endIndex:int = str.length - 1;
	        while (isWhitespace(str.charAt(endIndex)))
	            --endIndex;
	
	        if (endIndex >= startIndex)
	            return str.slice(startIndex, endIndex + 1);
	        else
	            return "";
   		}
   		 /**
	     * 去除字符串中所有空格 
	     * @param str 字符串
	     * @return 
	     * 
	     */	    
	    public static function clearBlank(str:String):String
	    {
	    	if(str == null) return null;
	    	while (str.indexOf(" ")!=-1){
				str=str.replace(" ","");
			}
			return str;
	    }
    	//是否是空格
		public static function isWhitespace(character:String):Boolean{
	        switch (character)
	        {
	            case " ":
	            case "\t":
	            case "\r":
	            case "\n":
	            case "\f":
	                return true;
	            default:
	                return false;
	        }
    	}
		/**
		 * 深度复制ByteArray
		 * @param value
		 * @return
		 *
		 */
		public static function copy(ba:ByteArray):ByteArray
		{
			var buffer:ByteArray=new ByteArray();
			buffer.writeBytes(ba,ba.position,ba.bytesAvailable);
			buffer.position=0;
			return buffer;
		}
		//获得指定长度字符
		public static function getStrByLen(str:String,len:int):String{
			if(str !=null && str!=null){
				return str.length > len ? str.substr(0,len)+"..." : str;
			}else{
				return "";
			}
		}
		
		public static function getSplitStr(front:String,back:String):String{
			front = (front != null ? front : "0");
			back = (back != null ? back : "0");
			return front +"/"+ back;
		}
		//随机数
		public static function ranRange(min:Number,max:Number):Number{
			return Math.floor( Math.random() * (max - min +1) ) + min;
		}
		
		public static function readXML(data:Object):XML{
			var ani:XML;
    		try{
    		 	var xmlBa:ByteArray = data as ByteArray;
    		 	//xmlBa.uncompress();
			 	ani = XML(xmlBa.readMultiByte(xmlBa.bytesAvailable,"utf-8"));
    		}catch(e:Error){
    			trace("解析数据出错");
    		}
    		return ani;
		}
		
		public static  function readString(byteArray:ByteArray):String{
			try{
				var len:int = byteArray.readShort();
				return byteArray.readMultiByte(len, "utf-8");
			}catch(e:Error){
				return "null";
			}
			return null;
		}
		
		/**
		 * 校验值 
		 * @param curValue
		 * @param maxValue
		 * @return 
		 * 
		 */		
		public static function checkValue(curValue:int,maxValue:int):int{
			if(curValue<0){
				curValue = 0;
			}else if(curValue>maxValue){
				curValue = maxValue;
			}
			return curValue;
		}
		
		/**
		 * 线性
		 * @param startValue 起始位置
		 * @param endValue 终止位置
		 * @param duration 起始位置移动到终止位置的持续时间 单位：毫秒
		 * @param time 间隔时间currentTime-startTime
		 * @return 当前位置
		 * 
		 */		
		public static function getLinerAlphaValue(startPoint:Point,endPoint:Point,duration:int,time:int):Point{
			var rate:Number = time/duration;
			if(rate>1){rate=1;}
			var x:int = rate*(endPoint.x-startPoint.x)+startPoint.x;
			var y:int = rate*(endPoint.y-startPoint.y)+startPoint.y;
		    return new Point(x,y);
		}
	}
}